package ${project.packageName}.${project.dataCenterType}.client.jedis;


import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import redis.clients.jedis.Jedis;

import ${project.packageName}.${project.dataCenterType}.client.jodis.JedisResourcePool;


/**
 * <p>Title: BONC - JedisPoolConfiguration </p> <p> <p>Description: JedisPool Spring boot构建类</p>
 * <p> <p>Copyright: Copyright BONC(c) 2013 - 2025 </p> <p> <p>Company: 北京东方国信科技股份有限公司 </p>
 *
 * @author yangdx
 * @version 1.0.0
 */
@Configuration
public class JedisPoolConfiguration implements EnvironmentAware {

    private final static Logger LOG = LoggerFactory.getLogger(JedisPoolConfiguration.class);

    RelaxedPropertyResolver resolver;

    @Override
    public void setEnvironment(Environment env) {
        // 获取所有的配置信息
        this.resolver = new RelaxedPropertyResolver(env, "JedisActionClient.java.");
    }

    @Bean(name = "jedisPool")
    public JedisResourcePool createJedisPool() {
        // LOG.debug("Configruing Redis Pool");
        // String zkPath = resolver.getProperty("server.zk.path");
        // String prodName = resolver.getProperty("server.product");
        //
        // Assert.notNull(zkPath, "zk path not be null");
        // Assert.notNull(prodName, "product not be null");
        //
        // LOG.debug("zk:{}, product:{}", zkPath, prodName);
        //
        // // Jedis 配置信息
        // JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // // 最大连接数, 应用自己评估
        // jedisPoolConfig.setMaxTotal(toInt(resolver.getProperty("pool.max.total", "1024")));
        // // 保持能空闲的最大对象数
        // jedisPoolConfig.setMaxIdle(toInt(resolver.getProperty("pool.max.idle", "512")));
        // jedisPoolConfig.setMinIdle(toInt(resolver.getProperty("pool.min.idle", "100")));
        //
        // return RoundRobinJedisPool.create().curatorClient(zkPath,
        // toInt(resolver.getProperty("server.zk.timeout", "3000"))).productName(prodName)
        // // .password(resolver.getProperty("server.password", ""))
        // .poolConfig(jedisPoolConfig).build();

        return new JedisResourcePool() {
            Jedis jedis = new Jedis("192.168.10.8", 6379);

            @Override
            public Jedis getResource() {
                return jedis;
            }

            @Override
            public void close()
                throws IOException {
                jedis.close();
            }
        };
    }

    /**
     * 转成Int类型
     *
     * @param val
     *            对应的值
     */
    private int toInt(String val) {

        return Integer.valueOf(val);
    }

}
